package com.angel.common.helpers;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;


/** The <code>NumberHelper.class</code> helps you to manage some short and dificults functions.
 *  All its methods are static.
 *
 * @author William
 * @version
 */
public class NumberHelper {
	/** It is the logger for the class. */
	private static Logger logger = Logger.getLogger(NumberHelper.class);

	private NumberHelper(){
		super();
	}

	/** Set current log level.
	 *
	 * @param logLevel
	 */
	public static void setLogLevel(Level logLevel){
		logger.setLevel(logLevel);
	}

	/** Return if a number is not pair.
	 *  To verify it, when the number is divide by two, the result must have decimals.
	 *
	 * @param number
	 * @return isNotPair
	 */
	public static boolean isNotPair(Number number){
		return divisionIsWithDecimals(number, 2);
	}

	/** Return if the number is a integer number.
	 *  If the the number in long value is equals to double value, it returns true. Otherwise it return false.
	 *
	 * @param number
	 * @return isInteger
	 */
	public static boolean isInteger(Number number){
		long longNumber = number.longValue();
		double doubleNumber = number.doubleValue();
		return (longNumber == doubleNumber);
	}

	/** Return if the number is between the minNumber, and the maxNumber inclusive. If it is like that, it returns true.
	 *  Otherwise it returns false.
	 *
	 * @param number
	 * @param minNumber
	 * @param maxNumber
	 * @return isBetween
	 */
	public static boolean isBetween(Number number, Number minNumber, Number maxNumber){
		return (number.intValue() >= minNumber.intValue()) && (number.intValue() <= maxNumber.intValue());
	}

	/** Return if a number is pair. First the number is divided by two, and this result is a number without decimal,
	 * it returns true. Otherwise it return false.
	 *
	 * @param number
	 * @return
	 */
	public static boolean isPair(Number number){
		return !divisionIsWithDecimals(number, 2);
	}

	/** Return if the result of divides numerador and denominador has decimals.
	 *
	 * @param numerador
	 * @param denominador
	 * @return isDivisionWithDecimal
	 */
	public static boolean divisionIsWithDecimals(int numerador, int denominador){
		return divisionIsWithDecimals((Number) numerador, (Number) denominador);
	}

	/** Verify if the result of division between numerador and denominador has decimals.
	 *
	 * @param numerador
	 * @param denominador
	 * @return It returns true if the result of the division has decimals. Otherwise it returns false.
	 */
	public static boolean divisionIsWithDecimals(Number numerador, Number denominador){
		double result = numerador.doubleValue() /  denominador.doubleValue() ;
		return hasDecimals(result);
	}

	/** Verify if the number as parameter has decimals.
	 *
	 * @param number
	 * @return It returns true if the number has decimals. Otherwise it returns false.
	 */
	private static boolean hasDecimals(Number number){
		Number minNumber = null;
		Number maxNumber = null;
		if(number.floatValue() > 0){
			minNumber = number.longValue();
			maxNumber = number.longValue() + 1;
		} else {
			minNumber = number.longValue() - 1;
			maxNumber = number.longValue();
		}
		return (number.doubleValue() > minNumber.floatValue() && number.doubleValue() < maxNumber.floatValue());
	}

	public static Long randomNumber(int digits){
		String number = RandomStringUtils.randomNumeric(digits);
		return Long.valueOf(number);

	}
}
